package top.hkyzf.study.leetcode.string;

import org.junit.Test;

/**
 * description: 
 * @author zhufeng
 * @date 2024/3/11 0011 9:38
 * @version V1.0
 */
public class 将标题首字母大写_2129 {
    public String capitalizeTitle(String title) {
        StringBuilder sb = new StringBuilder();
        String[] titles = title.split(" ");
        for (int j = 0; j < titles.length; j++) {
            String tit = titles[j];
            for (int i = 0; i < tit.length(); i++) {
                if (tit.length() <= 2) {
                    if (tit.charAt(i) <= 90) {
                        sb.append((char)(tit.charAt(i) + 32));
                    } else {
                        sb.append(tit.charAt(i));
                    }
                } else {
                    if (i == 0) {
                        if (tit.charAt(i) <= 90) {
                            sb.append(tit.charAt(i));
                        } else {
                            sb.append((char)(tit.charAt(i) - 32));
                        }
                    } else {
                        if (tit.charAt(i) <= 90) {
                            sb.append((char)(tit.charAt(i) + 32));
                        } else {
                            sb.append(tit.charAt(i));
                        }
                    }
                }
            }
            if (j != titles.length - 1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    @Test
    public void testCapitalizeTitle () {
        String title = "capiTalIze tHe A aS titLe";
        System.out.println(capitalizeTitle(title));
    }
}
